<!--
 * @Descripttion: 
 * @version: 
 * @Author: sueRimn
 * @Date: 2020-10-20 16:18:30
 * @LastEditors: sueRimn
 * @LastEditTime: 2020-11-20 16:13:18
-->
<template>
    <div class="mainBox">
        <el-page-header @back="goBack" content="加入班级" class="pageHeader">
        </el-page-header>
        <div class="selectSearchBox">
            <el-select
                v-model="classType"
                placeholder="请选择"
                size="mini"
                style="width: 150px;margin-right: 10px;"
                @change="getDataList"
            >
                <el-option
                    v-for="item in options"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                >
                </el-option>
            </el-select>
            <el-input
                placeholder="请输入您要查找的班级"
                size="mini"
                style="width: 200px;margin-right: 10px;"
                v-model="search_Grade_name"
            ></el-input>
            <el-button
                type="primary"
                icon="el-icon-search"
                size="mini"
                style="background: #018854;"
                @click="getDataList"
                >搜索</el-button
            >
        </div>
        <div class="searchClassBox">
            <div
                class="searchClassItemBox"
                v-for="(el, index) in dataList"
                :key="index"
            >
                <div class="itemBoxHeader">{{ el.group_name }}</div>
                <hr />
                <div class="itemBoxBody">
                    <div class="imgBox">
                        <img src="../../assets/images/icon/classList.png" />
                    </div>
                    <div class="itemBoxBodyContent">
                        <div class="teacher">
                            班主任：{{ el.nicheng ? el.nicheng : "未设置昵称" }}
                        </div>
                        <div>学生：{{ el.count_num }}人</div>
                        <div>{{ el.creat_time | getTime }}</div>
                    </div>
                </div>
                <div
                    class="itemBoxBottom isActive"
                    v-if="el.apply_state == 1"
                    @click="back(el.groupid, index)"
                >
                    撤销申请
                </div>
                <div
                    class="itemBoxBottom isJion"
                    v-else-if="el.apply_state == 2"
                >
                    已加入
                </div>
                <div
                    class="itemBoxBottom"
                    v-else
                    @click="jionIn(el.groupid, index)"
                >
                    申请加入
                </div>
            </div>
        </div>
        <div class="paginationBox">
            <el-pagination
                :page-size="pageSize"
                :pager-count="11"
                :current-page="page"
                layout="prev, pager, next"
                :total="total"
                @current-change="handleCurrentChange"
            >
            </el-pagination>
        </div>
    </div>
</template>

<script>
import { getGradeSearch, GradeJoin, GradeRevoke } from "@/api/class/index";
import { getFormatTime } from "@/utils/timeUtils";
export default {
    data() {
        return {
            options: [
                {
                    value: "0",
                    label: "学校班级"
                },
                {
                    value: "1",
                    label: "所有班级"
                }
            ],
            total: 0,
            page: 1,
            pageSize: 12,
            classType: "0",
            search_Grade_name: "",
            dataList: []
        };
    },
    filters: {
        getTime(val) {
            if (val != "") {
                return getFormatTime(val * 1000);
            }
        }
    },
    created() {
        this.getDataList();
    },
    methods: {
        goBack() {
            this.$router.go(-1);
        },
        // 获取班级列表
        async getDataList() {
            var query = {
                group_type: 1,
                is_state: 2,
                pagesize: this.pageSize,
                page: this.page
            };
            if (this.classType == "0") {
                query.parent_id = this.$store.getters.userInfo.school.admin_id;
            }
            if (this.search_Grade_name != "") {
                query.search_Grade_name = this.search_Grade_name;
            }
            var data = await getGradeSearch(query);
            if (data.code == 200) {
                this.total = data.data.total;
                this.dataList = data.data.data;
            }
            console.log("班级列表：", query, data);
        },
        async jionIn(id, index) {
            var query = {
                groupid: id
            };
            var data = await GradeJoin(query);
            if (data.code == 200) {
                this.dataList[index].apply_state = 1;
                this.$message({
                    message: "申请成功",
                    customClass: "customClass",
                    type: "success"
                });
            }
            console.log("加入班级：", data);
        },
        async back(id, index) {
            var query = {
                groupid: id
            };
            var data = await GradeRevoke(query);
            if (data.code == 200) {
                this.dataList[index].apply_state = 0;
                this.$message({
                    message: "撤回成功",
                    customClass: "customClass",
                    type: "success"
                });
            }
            console.log("加入班级：", data);
        },
        handleCurrentChange(val) {
            this.page = val;
            this.getDataList();
        }
    }
};
</script>

<style scoped lang="scss">
.mainBox {
    width: 100%;
    padding: 26px;
    box-sizing: border-box;

    .pageHeader {
        font-weight: bold;
        font-size: 20px;
        color: #2d3440;
    }

    .selectSearchBox {
        width: 100%;
        background-color: #ffffff;
        height: 50px;
        border-radius: 8px;
        margin-top: 20px;
        display: flex;
        align-items: center;
        padding: 5px 20px;
        box-sizing: border-box;
    }
}

.searchClassBox {
    width: 100%;
    margin-top: 20px;
    display: flex;
    flex-wrap: wrap;

    .searchClassItemBox {
        width: 32%;
        height: 197px;
        background-color: #ffffff;
        border-radius: 8px;
        overflow: hidden;
        position: relative;
        margin: 7.6px;

        .itemBoxHeader {
            height: 20px;
            line-height: 20px;
            color: #2d3440;
            font-size: 15px;
            margin: 10px 15px 0 15px;
        }

        .itemBoxBody {
            display: flex;
            padding: 10px 20px;
            box-sizing: border-box;
            .imgBox {
                background: #018854;
                border-radius: 50%;
                width: 55px;
                height: 55px;
                display: flex;
                justify-content: center;
                align-items: center;
                img {
                    width: 40px;
                    height: 40px;
                }
            }

            .itemBoxBodyContent {
                margin-left: 20px;
                color: #333;
                font-size: 14px;

                .teacher {
                    font-size: 15px;
                    color: rgba(0, 0, 0, 0.85);
                    font-weight: 600;
                }

                div {
                    margin-bottom: 8px;
                }
            }
        }

        .itemBoxBottom {
            width: 100%;
            height: 40px;
            line-height: 40px;
            position: absolute;
            bottom: 0;
            left: 0;
            border-top: #dadfe6 1px solid;
            background: #f5fcff;
            font-size: 15px;
            font-weight: 600;
            color: #018854;
            text-align: center;
            cursor: pointer;
        }
        .isActive {
            background: rgb(250, 181, 181);
            color: #ffffff;
        }
        .isJion {
            background: #018854;
            color: #ffffff;
        }
    }
}
.paginationBox {
    width: 100%;
    padding: 10px;
    box-sizing: border-box;
    background-color: #ffffff;
    text-align: center;
    margin-top: 20px;
    border-radius: 8px;
}
</style>
